//过滤器-截取字符串
template.defaults.imports.sub_str = function(data,length){
    return data.substr(0,length);
}
var baseUrl ="http://127.0.0.1:3000";

// 1.推荐列表
getRecommentList();
async function getRecommentList(){
    try {
        let res = await axios({url:baseUrl+"/home/recommend"});
        // console.log(res)
        var data = res.data ||[];// 接口返回的数据{msg:'',code:0，result：[...]}
        //成功，渲染列表
        if(data.code == 0){
            var list = data.result;
            // console.log(list)
            var li = "";
            list.forEach((item,index)=>{
                var date = dayjs.unix(item.ctime).format("YYYY-MM-DD HH:mm:ss");
                li+=`
                    <li>
                    <h3><a href="detail.html?id=${item.id}">${item.title}</a></h3>
                    <div class="census">
                        <span><i class="fa fa-calendar"></i>${date}</span>
                        <span><i class="fa fa-eye"></i>${item.view}</span>
                        <span><i class="fa fa-heart"></i>${item.like}</span>
                        <span><i class="fa fa-user"></i>${item.username}</span>
                    </div>
                    <div class="info">
                        <div class="pic">
                            <a href="detail.html?id=${item.id}">
                                <img src="${item.pic}">
                            </a>
                        </div>
                        <p>
                            ${item.sub_title}...
                            <a href="detail.html?id=${item.id}">详细</a>
                        </p>
                    </div>
                </li>
                `
            })
            // console.log(li)
            var box = document.querySelector(".main_left");
            var ul = box.children;
            // console.log(ul[0])
            ul[0].innerHTML = li;
        }

    } catch (error) {
        console.log(error)
    }
}

// 2.点赞
// getLikeList();// getLikeList is not a function
var getLikeList = async ()=>{
    let res = await axios.get(baseUrl+"/home/like");
    // console.log(res)
    var data = res.data ||[];

    //模板引擎渲染引擎
    //1.定义模板(index.html))
    //2.使用template 编译模板
    var page_data = {
        result:data.result
    }
    var li = template('like_list',page_data);//获取模板，编译成字符串
    //3.生成内容
    document.querySelector("#like_ul").innerHTML = li;

}
getLikeList();